# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/7/24

library(plyr)
library(dplyr)
library(tidyr)
library(tibble)
library(gridExtra)
library(purrr)
library(scales)
library(egg)
library(VennDiagram)
library(optparse)

createWhenNoExist <- function(f){
    ! dir.exists(f) && dir.create(f)
}

option_list <- list(
make_option("--i", default = "AllMet.csv", type = "character", help = "metabolite data file"),
make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file")
)
opt <- parse_args(OptionParser(option_list = option_list))

sampleInfo <- read.csv(opt$g, header = T, stringsAsFactors = F) %>%
select(c("SampleID", "ClassNote"))

classNotes <- sampleInfo %>%
.$ClassNote %>%
unique()
cn <- combn(classNotes, 2)
for (i in 1 : ncol(cn)) {
    row <- cn[, i]
    group1Name <- row[1]
    group2Name <- row[2]
    parent <- paste0(group2Name, "_", group1Name)
    createWhenNoExist(parent)
    diffSourceFileName <- paste0("../09/", parent, "/Diff_", group2Name, "_", group1Name, ".csv")
    diffTargetFileName <- paste0(parent, "/Diff_", group2Name, "_", group1Name, ".csv")
    file.copy(diffSourceFileName, diffTargetFileName)
    vennSourceFileName <- paste0("../09/", parent, "/Univariate_and_Multivariate_Venn_Plot_", group2Name, "_", group1Name, ".pdf")
    vennTargetFileName <- paste0(parent, "/Univariate_and_Multivariate_Venn_Plot_", group2Name, "_", group1Name, ".pdf")
    file.copy(vennSourceFileName, vennTargetFileName)
}





